package zhcms.framework;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;

import zhcms.framework.dao.GyFor;
import zhcms.framework.dao.GyList;

public class GyT1 {
	// 引用的文件
	public String gytFile = "";
	private String gytContent = "";
	private String gytFrontSeparator = "{";
	private String gytBackSeparator = "}";
	private String[] gytParameterArray;

	public HashMap<String, String> gytInVar;
	public HashMap<String, ArrayList<HashMap<String, String>>> gytInFor;
	public HashMap<String, String> gytInIf;

	public GyT1() {
		gytInVar = new HashMap<String, String>();
		gytInFor = new HashMap<String, ArrayList<HashMap<String, String>>>();
		gytInIf = new HashMap<String, String>();
	}

	public void setFrontSeparator(String gytemp) {
		this.gytFrontSeparator = gytemp;
	}

	public void setBackSeparator(String gytemp) {
		this.gytBackSeparator = gytemp;
	}

	public void setfilename(String gytfile) {
		this.gytFile = gytfile;
	}

	@SuppressWarnings("unused")
	private String getfilename() {
		return this.gytFile;
	}

	@SuppressWarnings("rawtypes")
	public HashMap setInVtrParameter(String ParameterName, String ParameterVale) {
		this.gytInVar.put(ParameterName, ParameterVale);
		return this.gytInVar;
	}

	@SuppressWarnings("rawtypes")
	public HashMap setInForParameter(String ParameterName,
			ArrayList<HashMap<String, String>> ParameterVale) {
		this.gytInFor.put(ParameterName, ParameterVale);
		return this.gytInFor;
	}

	@SuppressWarnings("rawtypes")
	public HashMap setInIfParameter(String ParameterName, String ParameterVale) {
		this.gytInIf.put(ParameterName, ParameterVale);
		return this.gytInIf;
	}

	private String readFile(String fileName) throws IOException {
		String filePath = this.getClass().getResource("").getPath().toString();
		// System.out.println("aaaa+"+filePath);
		// String
		// filePath=this.getClass().getClassLoader().getResource("").getPath().toString();
		// System.out.println(filePath);
		if (filePath.indexOf("WEB-INF") > 0) {
			// filePath =
			// filePath.substring(0,filePath.indexOf("/WEB-INF/classes"));
			GySplit b = new GySplit();
			b.tool(filePath, "/WEB-INF");
			String[] temp_array = b.Show();
			b = null;
			filePath = temp_array[0];
		}

		// System.out.println(filePath);
		filePath = filePath + "/" + fileName;
		File f = new File(filePath);
		// FileReader fr = new FileReader(f);
		// StringBuffer content = new StringBuffer();
		String content = "";
		// for (int c; (c = fr.read()) != -1; content.append((char)c));
		// fr.close();

		StringBuilder strContent1 = new StringBuilder("");
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(f)));
			String line = null;
			while ((line = br.readLine()) != null) {
                strContent1.append(line.toString());
    			strContent1.append("\n");
			}
			br.close();
		} catch (IOException e) {

		}

		return strContent1.toString();
	}

	private String readFile(String fileName, String iCharset)
			throws IOException {
		String filePath = this.getClass().getResource("").getPath().toString();
		// System.out.println("aaaa+"+filePath);
		// String
		// filePath=this.getClass().getClassLoader().getResource("").getPath().toString();
		// System.out.println(filePath);
		if (filePath.indexOf("WEB-INF") > 0) {
			// filePath =
			// filePath.substring(0,filePath.indexOf("/WEB-INF/classes"));
			GySplit b = new GySplit();
			b.tool(filePath, "/WEB-INF");
			String[] temp_array = b.Show();
			b = null;
			filePath = temp_array[0];
		}

		//System.out.println(filePath);
		filePath = filePath + "/" + fileName;
		//System.out.println("aaaa+"+filePath);
		File f = new File(filePath);
		// FileReader fr = new FileReader(f);
		// StringBuffer content = new StringBuffer();
		String content = "";
		// for (int c; (c = fr.read()) != -1; content.append((char)c));
		// fr.close();

		StringBuilder strContent1 = new StringBuilder("");
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(f), iCharset));
			String line = null;
			while ((line = br.readLine()) != null) {
				strContent1.append(line.toString());
        		strContent1.append("\n");
			}
			br.close();
		} catch (IOException e) {

		}

		return strContent1.toString();
	}

	public void parse(String iCharset) {
		//System.out.println("代码执行次数1：：");
		try {
			this.gytContent = readFile(this.gytFile, iCharset);
			//System.out.println(this.gytFile);
			GySplit a = new GySplit();
			a.tool(this.gytContent, this.gytFrontSeparator);
			String[] tempContent = a.Show();
			a = null;
			// String[]
			// tempContent=this.gytContent.split(this.gytFrontSeparator);
			// System.out.println(tempContent[0]);
			int temp_count = tempContent.length;
			//System.out.print(this.gytContent);
			if(temp_count<=1)
			{
				//this.gytContent = this.gytContent;
			}
			else
			{
				this.gytParameterArray = new String[temp_count - 1];
				if (temp_count > 0) {
					// 得到标签
					for (int i = 1; i < temp_count; i++) {
						GySplit b = new GySplit();
						b.tool(tempContent[i], this.gytBackSeparator);
						String[] temp_end_array = b.Show();
						b = null;
						// String[]
						// temp_end_array=tempContent[i].split(this.gytBackSeparator);
						this.gytParameterArray[i - 1] = temp_end_array[0];
						 //System.out.println(this.gytParameterArray[i - 1]);
					}
				}
			
				// 处理include标签
				RepInclude();
				
				// 重新获取标签
				this.gytParameterArray = null;
				tempContent = null;
				temp_count = 0;

				GySplit c = new GySplit();
				c.tool(this.gytContent, this.gytFrontSeparator);
				tempContent = c.Show();
				c = null;

				// tempContent=this.gytContent.split(this.gytFrontSeparator);
				temp_count = tempContent.length;
				this.gytParameterArray = new String[temp_count - 1];
				if (temp_count > 0) {
					// 得到标签
					for (int i = 1; i < temp_count; i++) {
						GySplit b = new GySplit();
						b.tool(tempContent[i], this.gytBackSeparator);
						String[] temp_end_array = b.Show();
						b = null;
						// String[]
						// temp_end_array=tempContent[i].split(this.gytBackSeparator);
						this.gytParameterArray[i - 1] = temp_end_array[0];
					}
				}
			
				// 处理判断
				RepIf(this.gytContent);
				// this.gytContent=RepIf();
				// 处理list循环
				RepList(this.gytContent);
				// 处理循环//
				RepFor(this.gytContent);
				// 处理替换的标签
				RepString();
				//System.out.println("查看源代码：："+this.gytContent);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void parse() {
		try {
			this.gytContent = readFile(this.gytFile);
			// System.out.println(this.gytContent);
			GySplit a = new GySplit();
			a.tool(this.gytContent, this.gytFrontSeparator);
			String[] tempContent = a.Show();
			a = null;
			// String[]
			// tempContent=this.gytContent.split(this.gytFrontSeparator);
			// System.out.println(tempContent[0]);
			int temp_count = tempContent.length;
			this.gytParameterArray = new String[temp_count - 1];
			if (temp_count > 0) {
				// 得到标签
				for (int i = 1; i < temp_count; i++) {
					GySplit b = new GySplit();
					b.tool(tempContent[i], this.gytBackSeparator);
					String[] temp_end_array = b.Show();
					b = null;
					// String[]
					// temp_end_array=tempContent[i].split(this.gytBackSeparator);
					this.gytParameterArray[i - 1] = temp_end_array[0];
					// System.out.println(temp_end_array[0]);
				}
			}
			// 处理include标签
			RepInclude();
			// 重新获取标签
			this.gytParameterArray = null;
			tempContent = null;
			temp_count = 0;

			GySplit c = new GySplit();
			c.tool(this.gytContent, this.gytFrontSeparator);
			tempContent = c.Show();
			c = null;

			// tempContent=this.gytContent.split(this.gytFrontSeparator);
			temp_count = tempContent.length;
			this.gytParameterArray = new String[temp_count - 1];
			if (temp_count > 0) {
				// 得到标签
				for (int i = 1; i < temp_count; i++) {
					GySplit b = new GySplit();
					b.tool(tempContent[i], this.gytBackSeparator);
					String[] temp_end_array = b.Show();
					b = null;
					// String[]
					// temp_end_array=tempContent[i].split(this.gytBackSeparator);
					this.gytParameterArray[i - 1] = temp_end_array[0];
				}
			}
			
			//System.out.println("查看源代码：："+this.gytContent);
			// 处理判断
			RepIf(this.gytContent);
			// this.gytContent=RepIf();
			// 处理list循环
			RepList(this.gytContent);
			// 处理循环
			RepFor(this.gytContent);
			// 处理替换的标签
			RepString();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 包含文件
	private String RepInclude() throws IOException {
		String tempS = "";
		tempS = this.gytContent;
		// 对所有标签进行循环，提取include
		for (int i = 0; i < this.gytParameterArray.length; i++) {
			if (this.gytParameterArray[i].length() > 7) {
				if (this.gytParameterArray[i].substring(0, 7).equals("include")) {
					GySplit a = new GySplit();
					a.tool(this.gytParameterArray[i], "=");
					String[] tempArray = a.Show();
					a = null;

					// String[] tempArray=this.gytParameterArray[i].split("=");
					String tempC = "";
					try {
						tempC = readFile(tempArray[1]);
					} catch (IOException e) {
						e.printStackTrace();
					}
					// 使用自定义函数进行字符串替换
					GyReplace r = new GyReplace();
					tempS = r.replace(tempS,
							this.gytFrontSeparator + "include=" + tempArray[1]
									+ this.gytBackSeparator, tempC);
					r = null;
					// tempS=tempS.replaceAll(this.gytFrontSeparator+"include="+tempArray[1]+this.gytBackSeparator,
					// tempC);
				}
			}
		}

		return tempS;
	}

	// 变量
	private void RepString() {
		String tempS="";
		tempS = this.gytContent;
		//System.out.println(tempS);
		for (int i = 0; i < this.gytParameterArray.length; i++) {
			//System.out.println("替换的字符串:::::::::::"+this.gytParameterArray[i]);
			// 对已经设置的进行循环替换
			Iterator<Entry<String, String>> iSer = this.gytInVar.entrySet()
					.iterator();
			while (iSer.hasNext()) {
				@SuppressWarnings("rawtypes")
				Map.Entry entry = (Map.Entry) iSer.next();
				// 查找是否存在，存在替换
				//System.out.println("要匹配的字符串:::::::::::"+this.gytParameterArray[i]);
				if (entry.getKey().toString().equals(this.gytParameterArray[i].toString())) {
					// 使用自定义函数进行字符串替换
					//System.out.println("匹配成功的字符串:::"+entry.getKey().toString());
					GyReplace r = new GyReplace();
					tempS = r.replace(tempS,this.gytFrontSeparator + this.gytParameterArray[i]	+ this.gytBackSeparator, entry.getValue().toString());
					// tempS=tempS.replace(this.gytFrontSeparator+this.gytParameterArray[i]+this.gytBackSeparator,
					// this.gytParameterArray[i]);
					r = null;
				}
			}
		}
		this.gytContent = tempS;
		//return tempS;
	}

	//list标签，--新增
	private void RepList(String tempS)
	{
			GyList gyl=new GyList();
			gyl.setContent(tempS);
			gyl.ParseList(this.gytFrontSeparator, this.gytBackSeparator);
			this.gytContent=gyl.getreturnS();
	}
	
	// 循环
	private void RepFor(String tempS) {
			//
		//System.out.println("执行一次：：");
			GyFor gyf=new GyFor();
			gyf.setContent(tempS);
			
			gyf.ParseFor(this.gytFrontSeparator, this.gytBackSeparator, gytInFor);
			this.gytContent=gyf.getreturnS();
			
	}
	
	private void RepIf(String tempS) {
		String[] gytParameterArray = null;
		String[] gyitParameterArray = null;// 当前的if标签
		Vector<String> v = new Vector<String>();
		String[] tempContent = null;
		int temp_count = 0;

		GySplit h = new GySplit();
		h.tool(tempS, this.gytFrontSeparator);
		tempContent = h.Show();
		h = null;

		temp_count = tempContent.length;
		gytParameterArray = new String[temp_count - 1];
		if (temp_count > 0) {
			// 得到标签
			for (int i = 1; i < temp_count; i++) {
				GySplit b = new GySplit();
				b.tool(tempContent[i], this.gytBackSeparator);
				String[] temp_end_array = b.Show();
				b = null;
				gytParameterArray[i - 1] = temp_end_array[0];
			}
		}
		for (int i = 0; i < gytParameterArray.length; i++) {
			if (gytParameterArray[i].substring(0, 3).toString().equals("if:")) {
				v.add(gytParameterArray[i]);
			}
		}

		if (v == null || v.size() == 0) {
			this.gytContent = tempS;
		} else {
			gyitParameterArray = new String[v.size()];

			for (int i = 0; i < gyitParameterArray.length; i++) {
				gyitParameterArray[i] = new String((String) v.elementAt(i));
			}
			v = null;

			// 循环的变量为gyitParameterArray[0]

			// String tempS="";
			String tempSpStart = "";// 循环前的字符串
			String tempSpEnd = "";// 循环后的字符串
			String tempSpIf = "";// 循环的字符串

			Iterator<Entry<String, String>> iSer = this.gytInIf.entrySet()
					.iterator();

			// 对for进行分割
			GySplit a = new GySplit();
			// System.out.println("1++"+"代码显示"+this.gytFrontSeparator+gyitParameterArray[0]+this.gytBackSeparator);
			a.tool(tempS, this.gytFrontSeparator + gyitParameterArray[0]
					+ this.gytBackSeparator);
			String[] tempSIfHead = a.Show();
			a = null;
			// String[]
			// tempSIfHead=tempS.split(this.gytFrontSeparator+this.gytParameterArray[i]+this.gytBackSeparator);
			// System.out.println("222++"+"代码显示"+tempSIfHead[1]);
			// 得到要判断的变量和值
			GySplit f = new GySplit();
			f.tool(gyitParameterArray[0], " ");
			String[] tempParameterArray = f.Show();
			f = null;
			// System.out.println("变量值："+gyitParameterArray[0]);
			GySplit b = new GySplit();
			b.tool(tempSIfHead[1], this.gytFrontSeparator + "end"
					+ tempParameterArray[0] + this.gytBackSeparator);
			String[] tempSIfEnd = b.Show();
			b = null;
			// System.out.println("1++"+"代码显示"+this.gytFrontSeparator+"end"+tempParameterArray[0]+this.gytBackSeparator);
			// String[]
			// tempSIfEnd=tempS.split(this.gytFrontSeparator+"end"+this.gytParameterArray[i]+this.gytBackSeparator);
			tempSpStart = tempSIfHead[0];// 开始的 内容
			tempSpIf = tempSIfEnd[0];// 要判断的内容

			for (int af = 1; af < tempSIfEnd.length; af++) {
				tempSpEnd = tempSpEnd + this.gytFrontSeparator + "end"
						+ tempParameterArray[0] + this.gytBackSeparator
						+ tempSIfEnd[af];
			}

			// System.out.println("尾部代码显示3~~~~~\n"+tempSpEnd);
			String tempsub = this.gytFrontSeparator + "end"
					+ tempParameterArray[0] + this.gytBackSeparator;

			tempSpEnd = tempSpEnd.substring(tempsub.length(),
					tempSpEnd.length());
			// tempSpEnd=tempSIfEnd[1];//结束的内容
			// System.out.println("尾部代码显示0~~~~~\n"+tempsub.length());
			// System.out.println("尾部代码显示1~~~~~\n"+tempSpEnd.length());
			// System.out.println("尾部代码显示2~~~~~\n"+tempSpEnd);
			tempSIfHead = null;
			tempSIfEnd = null;
			// 对判断体进行替换相关的内容，然后重新封装输出字符串
			// System.out.println("尾部代码显示"+tempSpEnd);
			// 得到要循环的list

			GySplit c = new GySplit();
			c.tool(tempParameterArray[0], ":");
			String[] tempIfParameterArray = c.Show();
			c = null;

			// System.out.println("1++"+"代码显示"+tempParameterArray[1]);
			while (iSer.hasNext()) {
				// 判断是否有没有输入的参数
				@SuppressWarnings("rawtypes")
				Map.Entry entry = (Map.Entry) iSer.next();
				// System.out.println("1++"+"代码显示"+entry.getKey().toString());
				// System.out.println("2++"+"代码显示"+tempIfParameterArray[1]);
				if (tempIfParameterArray[1].equals(entry.getKey().toString())) {
					// 找到输入的参数
					// 进行输处理
					// 得到要判断的变量的值
					String gytempA = entry.getValue().toString();

					String tempSS = "";
					// 对中间内容进行分割，然后替换
					if (tempSpIf.indexOf(this.gytFrontSeparator + "else:"
							+ tempIfParameterArray[1] + this.gytBackSeparator) > 0) {

						GySplit e = new GySplit();
						e.tool(tempSpIf, this.gytFrontSeparator + "else:"
								+ tempIfParameterArray[1]
								+ this.gytBackSeparator);
						String[] tempSSIf = e.Show();
						e = null;
						// System.out.println("变量值0："+tempParameterArray.length);
						// System.out.println("变量值1："+tempParameterArray[1].toString());
						// System.out.println("变量值2："+gytempA);
						if (tempParameterArray[1].toString().equals(gytempA)) {
							tempSS = tempSSIf[0];
						} else {
							tempSS = tempSSIf[1];
						}
					} else {
						tempSS = tempSpIf;
					}
					// System.exit(0);
					this.gytContent = tempSpStart + tempSS + tempSpEnd;
					// System.out.println("全部代码显示"+this.gytContent);
					RepIf(this.gytContent);
				}
			}
		}
	}

	public String Out() {
		return null;
		//return this.gytContent;
	}

	public String test() {
		return "guoyong";
	}
}